using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HCManage.Api.Models;
using HCManage.Api.Models.Fileter;
using HCManage.IBll;
using HCManage.Model;
using Microsoft.AspNetCore.Mvc;

namespace HCManage.Api.Controllers.BaseInfo
{

 
    [Route("api/[controller]")]
    [ApiController]
    public class PageController : Controller
    {
        #region 模块

        private readonly ITblPageBll pageBll;
        private DataResultModel dataResult = new DataResultModel();
        #endregion


        #region 构造函数注入
        public PageController(ITblPageBll page_Bll)
        {
            this.pageBll = page_Bll;
        }
        #endregion


        /// <summary>
        /// 根据id查询页的单个信息
        /// </summary>
        /// <param name="id">键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ResultExceptionFilter]
        public JsonResult Get(string id) 
        {
            tbl_page model = pageBll.Get(id);
            dataResult.success = true;
            dataResult.data = model;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 根据页列表的所有信息
        /// </summary>
        /// <param name="page_name">頁面名稱</param>
        /// <param name="page">页码</param>
        /// <param name="page_size">每页显示的条数</param>
        /// <returns></returns>
        [HttpGet]
        [ResultExceptionFilter]
        public JsonResult List(string page_name,string sys_no, int? page = 1, int? page_size = 30)
        {
            Tuple<List<tbl_page>, int> result = pageBll.List(page_name,sys_no, page.Value, page_size.Value);
            dataResult.success = true;
            dataResult.data = result.Item1.Select(s => new
            {
                s.page_no,
                s.page_name,
                s.sys_no,
                s.page_pno,
                s.creater,
                s.create_date,
                s.editer,
                s.edit_date,
                s.page_orderby
            });
            dataResult.total = result.Item2;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 添加页数据
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult Post(tbl_page model) 
        {
            if (model == null)
            {
                throw new Exception("请传入参数");
            }
            bool success = pageBll.Insert(model);
            if (!success) 
            {
                throw new Exception("新增失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 更新页数据
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPut]
        [ResultExceptionFilter]
        public JsonResult Put(tbl_page model) 
        {
            if (model == null)
            {
                throw new Exception("请传入参数");
            }
            bool success = pageBll.Update(model);
            if (!success)
            {
                throw new Exception("更新失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 删除页数据
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpDelete]
        [ResultExceptionFilter]
        public JsonResult Delete(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new Exception("请传入参数");
            }
            bool success = pageBll.Delete(id);
            if (!success)
            {
                throw new Exception("删除失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }
    }
}
